package edu.bu.cs673.superkids123.children.model;
import javax.swing.ImageIcon;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
//Is this OK? or bad practice?
//My model and the question generator are combined into 1
//Decided that this was correct based on common Rails practice to make models heavy and controllers light
public class ArithmaticQuestion {

	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
	private int intA;
	private int intB;
	private int range;
	private int[] options;
	private int correctAnswerLocation;
	
	public ArithmaticQuestion(int age, int numOptions){
		options = new int[numOptions+1];
		this.correctAnswerLocation = 1 + (int) (Math.random() * (numOptions-1) );
		if ( age==2 ){
			range = 8;
			intA = (int) (Math.random() * 3);
			intB = (int) (Math.random() * 4);
		}
		else if ( age == 3 ){
			range = 15;
			intA = (int) (Math.random() * 8);
			intB = (int) (Math.random() * 3);
		}
		else if ( age == 4 ){
			range = 20;
			intA = (int) (Math.random() * 15);
			intB = (int) (Math.random() * (15-intA));
		}	
		else {
			range = 20;
			intA = (int) (Math.random() * 21);
			intB = (int) (Math.random() * (21-intA));
		}
		for(int i=1; i<=numOptions; i++){
			if (i==correctAnswerLocation) options[i]=getAnswer();
			else {
				int wrongAnswer=0; 
				boolean invalid = true;
				while (invalid){
					wrongAnswer = (int) (Math.random() * (range+1));
					if(wrongAnswer != getAnswer()) invalid = false;
				}
				options[i] = wrongAnswer;
			}
		}
	}
	public ImageIcon getImageA() {
		return provider.getScaledImage(ChildrenResource.NUMBER_IMAGE_MAP.get(intA), (float) 0.5);
	}
	public ImageIcon getImageB() {
		return provider.getScaledImage(ChildrenResource.NUMBER_IMAGE_MAP.get(intB), (float) 0.5);
	}
	public int getA(){
		return intA;
	}
	public int getB(){
		return intB;
	}
	public int getAnswer() {
		return intA + intB;
	}
	public int getCorrectAnswerLocation(){
		return correctAnswerLocation;
	}
	public ImageIcon atAnswerOption(int requestedLocation) {
		return provider.getScaledImage(ChildrenResource.NUMBER_IMAGE_MAP.get(options[requestedLocation]), (float) .5);
	}
	public int getOption(int optionLocation){
		return options[optionLocation];
	}

}
